<template>
	<div class="theme">
		<van-nav-bar :title="$t('UpdatePwd.v1')" :left-text="$t('MyDividend.v6')" left-arrow @click-left="$router.push({path: '/Safety'})" />
		<div class="overlay headTop">
			<div class="overlay-content scrolling" align="center" style="padding: 10px 10px;">
				<van-cell-group>
					<van-field :placeholder="$t('UpdatePwd.v2')" maxlength="20" type="password" v-model.trim="oldPwd" />
				</van-cell-group>
				<van-cell-group>
					<van-field :placeholder="$t('UpdatePwd.v3')" maxlength="20" type="password" v-model.trim="newPwd" />
				</van-cell-group>
				<van-cell-group>
					<van-field :placeholder="$t('UpdatePwd.v4')" maxlength="20" type="password" v-model.trim="new2Pwd" />
				</van-cell-group>
				<div class="btn">
					<van-button type="primary" @click.native="sure" :loading="loading" style="width: 100%;">{{$t('UpdatePwd.v5')}}</van-button>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	import { base, safety } from './../utils/api';
	let _this;
	export default {
		data() {
			return {
				oldPwd: '', // 旧密码
				newPwd: '', // 旧密码
				new2Pwd: '', // 旧密码
				k: '', // 凭证K值
				v: '', // 凭证V值
				loading: false
			}
		},
		beforeCreate() {
			_this = this;
		},
		mounted() {

		},
		methods: {
			sure() {
				if(this.oldPwd == '') {
					this.$toast(this.$t('UpdatePwd.v6'));
					return false;
				}
				if(this.oldPwd.length < 6 || this.oldPwd.length > 20) {
					this.$toast(this.$t('UpdatePwd.v7'));
					return false;
				}
				if(this.newPwd == '') {
					this.$toast(this.$t('UpdatePwd.v8'));
					return false;
				}
				if(this.newPwd.length < 6 || this.newPwd.length > 20) {
					this.$toast(this.$t('UpdatePwd.v9'));
					return false;
				}
				if(this.newPwd != this.new2Pwd) {
					this.$toast(this.$t('UpdatePwd.v10'));
					return false;
				}
				this.loading = true;
				base.getCredentials().then(res => {
					if(res.code == 8008) {
						this.k = res.data.k;
						this.v = res.data.v;
						safety.getUpdatePassword({
							'key': this.k,
							'encryptedData': this.rsaEncrypt(this.oldPwd + ',' + this.newPwd + ',' + this.v)
						}).then(res => {
							if(res.code == 200) {
								setTimeout(() => {
									this.loading = false;
									base.getSignOut().then(res => {
										sessionStorage.clear();
										localStorage.clear();
										_this.axios.defaults.headers['Authorization'] = '';
										_this.$router.push({
											path: '/Login'
										});
									})
								}, 500);
							} else if(res.code == 500) {
								this.loading = false;
							}
							this.$toast(res.msg);
						})
					}
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	/deep/ .weui-btn_primary {
		background: #3fb776 !important;
	}
	
	.btn {
		padding: 30px 10px 0px;
	}
	
	.van-cell {
		font-size: 15px;
		padding: 15px;
	}
</style>